package com.stadium.handler;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.stadium.bean.Match;
import com.stadium.servlet.MatchServlet;
import com.stadium.tools.ResultMessage;



@Controller
@RequestMapping(value = "views")
public class MatchHandler {

	@Autowired
	private MatchServlet matchServlet;

	private final Logger log = LoggerFactory.getLogger(MatchHandler.class);

	/*
	 * 获取所有的赛事信息
	 */
	@RequestMapping(value = "/findAllMatchs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findAllMatchs(Integer type) {
		log.info("获取所有的赛事信息");
		try {
			ResultMessage result = new ResultMessage();
			List<Match> page = matchServlet.findAll(type);
			result.getResultParm().put("match", page);
			result.setResultInfo("获取所有的赛事信息");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("查询失败");
		}
	}

	/*
	 * 添加新的赛事申请
	 */
	@RequestMapping(value = "/addMatch", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> addMatch(Match match) throws ParseException {
		log.info("添加新的赛事申请");
		ResultMessage result = new ResultMessage();
		System.out.println(match);
		matchServlet.addMatch(match);
		result.setResultInfo("添加新的赛事申请");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);

	}

	/*
	 * 添加新的赛事申请
	 */
	@ModelAttribute
	public void getUserbyupdata(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		if (id != null) {
			map.put("match", (Object) matchServlet.findOne(id));
		}
	}
	@RequestMapping(value = "/updateMatchOrder", method = RequestMethod.POST)
	public ResponseEntity<ResultMessage> updateMatchOrder(Match match) throws ParseException {
		log.info("更新赛事申请");
		ResultMessage result = new ResultMessage();
		matchServlet.updateMatchOrder(match);
		result.setResultInfo("更新赛事申请");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);

	}
	
	/*
	 * 更新赛事状态
	 */
	@RequestMapping(value = "/updataMatch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> updataMatch(Integer matchId, Integer type) {
		log.info("更新赛事状态");
		ResultMessage result = new ResultMessage();
		try {
			System.out.println(matchId+"matchId");
			System.out.println(type+"type");
			matchServlet.setType(matchId, type);
			result.setResultInfo("更新赛事状态");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("更新失败");
		}
	}

	/*
	 * 删除赛事操作
	 */
	@RequestMapping(value = "/deleteMatch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> deleteMatch(Integer matchId) {
		log.info("删除赛事申请");
		ResultMessage result = new ResultMessage();
		matchServlet.deleteMatch(matchId);
		result.setResultInfo("删除赛事申请成功");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);

	}
	
	
	/*
	 * 通过搜索名字获取所有的场地
	 */
	@RequestMapping(value = "/findMatches", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findAllByName() {
		log.info("获取所有的赛事申请信息");
		try {
			// 获取session，并在session中获取account的值
			Subject subject = SecurityUtils.getSubject();
			Session session = subject.getSession();
			String account = (String) session.getAttribute("account");
			ResultMessage result = new ResultMessage();
			result.getResultParm().put("places", matchServlet.findAllByBorrowAccount(account));
			result.setResultInfo("获取所有的赛事申请信息");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("查询失败");
		}
	}
	
	/*
	 * 获取所有的赛事信息
	 */
	@RequestMapping(value = "/findMatchByName", produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<ResultMessage> findMatchByName(String name) {
		log.info("获取所有的赛事信息通过名称");
		try {
			ResultMessage result = new ResultMessage();
			Match page = matchServlet.findMatchByName(name);
			result.getResultParm().put("match", page);
			result.setResultInfo("获取所有的赛事信息");
			return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SecurityException("查询失败");
		}
	}

	
}
